package com.zmn.plat.admin.controller.orderconfig.distribute;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.manager.redis.RedisManager;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.common.constant.DpermitConsts;
import com.zmn.mcc.common.dto.permit.SpermitDTO;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.mcc.dubbo.interfaces.staff.StaffPermitRemoteService;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.orderconfig.distribute.DistributeWeightPlatBService;
import com.zmn.plat.business.interfaces.orderconfig.distribute.RobOrderConfigBService;
import com.zmn.plat.common.constant.CompanyConsts;
import com.zmn.plat.model.entity.company.BaseCompany;
import com.zmn.plat.model.entity.company.BaseCompanyQuery;
import com.zmn.plat.model.entity.company.CompanyTypeQuery;
import com.zmn.plat.services.interfaces.area.BaseAreaService;
import com.zmn.plat.services.interfaces.company.BaseCompanyAreaService;
import com.zmn.plat.services.interfaces.company.BaseCompanyService;
import com.zmn.plat.services.interfaces.orderconfig.distribute.DistributeWeightCompanyService;
import com.zmn.plat.services.interfaces.orderconfig.distribute.DistributeWeightPlatService;
import com.zmn.plat.services.interfaces.orderconfig.distribute.DistributeWeightRelationService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 类描述：派单参数配置基础控制器
 *
 * @author mujunlin
 * @version v1.0
 * @since 2019/10/17 14:15
 */
public class DistributeWeightBaseController extends PlatBaseController {

    @Resource
    protected BaseAreaService baseAreaService;
    @Resource
    protected BaseCompanyService baseCompanyService;
    @Resource
    protected BaseCompanyAreaService baseCompanyAreaService;
    @Resource
    protected DistributeWeightPlatBService distributeWeightPlatBService;
    @Resource
    protected DistributeWeightCompanyService distributeWeightCompanyService;

    @Resource
    protected DistributeWeightPlatService distributeWeightPlatService;

    @Resource
    protected DistributeWeightRelationService distributeWeightRelationService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    StaffPermitRemoteService staffPermitRemoteService;

    @Resource
    protected RobOrderConfigBService robOrderConfigBService;

    @Resource
    protected RedisManager redisManager;

    /**
     * 根据员工ID获取员工拥有的平台权限
     *
     * @param staff
     *            员工
     * @return 拥有权限的平台ID集合
     */
    protected List<Integer> listPermitedPlatByStaff(LoginStaff staff) {

        SpermitDTO spermitDTO = staffPermitRemoteService.listDpermitByLoginStaff(staff, DpermitConsts.TYPE_ORGANIZE_PLAT, null);

        // 无数据权限
        if (Objects.equals(spermitDTO.getRange(), SpermitDTO.RANGE_EMPTY)) {
            return Collections.emptyList();
        }

        // 全部数据权限
        if (Objects.equals(spermitDTO.getRange(), SpermitDTO.RANGE_ALL)) {
            return null;
        }

        // 正常权限 但是权限数据集合为空 视为 无权限则直接返回
        if (Objects.equals(spermitDTO.getRange(), SpermitDTO.RANGE_NORMAL) && CollectionUtils.isEmpty(spermitDTO.getItems())) {
            return Collections.emptyList();
        }

        // 正常权限
        return spermitDTO.getItems();

    }

    /**
     * 加载所有城市
     *
     * @return 所有城市信息
     */
    List<VtDTO> loadAllCityData() {
        return baseAreaService.listAllCity().stream().map(item -> new VtDTO(item.getKey(), item.getValue())).collect(toList());
    }

    /**
     * 根据平台查询平台下的公司
     *
     * @param plat
     *            平台ID
     * @return 平台下的公司集合
     */
    List<VtDTO> listCompanyByPlat(Integer plat) {

        List<CompanyTypeQuery> typeQueryList = Arrays.asList(
                new CompanyTypeQuery(CompanyConsts.COMPANY_TYPE_SERVICE, CompanyConsts.COMPANY_TYPE_SERVICE_OCO),
                new CompanyTypeQuery(CompanyConsts.COMPANY_TYPE_SERVICE, CompanyConsts.COMPANY_TYPE_SERVICE_NOT_OCO));
        List<BaseCompany> companyList = baseCompanyService.listByPlatAndCompanyTypeQuery(plat, typeQueryList);
        return companyList.stream().map(item -> new VtDTO(item.getCompanyId(), item.getName())).collect(Collectors.toList());
    }

}
